package com.ruoyi.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.entity.Order;
import com.ruoyi.service.OrderService;
import com.ruoyi.utils.R;
import com.ruoyi.websocket.OrderChanel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisAccessor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author : Xuzhaobo
 * @date : 2024-12-20 18:55
 * @description :
 **/
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
     OrderService orderService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;



    @PostMapping("/takeOrder")
    public R takeOrder(@RequestBody Order order){
        System.out.println("司机:"+order.getCarCode()+"抢单:"+order.getOrderId());

        redisTemplate.opsForList().leftPush("takeOrder:"+order.getOrderId(),order.getCarCode());

        return R.OK("抢单成功");

    }

    @GetMapping("sendDriver")
    public R sendDriver() throws IOException {
        String carCode="京NZ01Y8";
        Order order = new Order();
        order.setOrderId(1);
        order.setStartingPlace("北京八维");
        order.setDestination("北京西站");
        order.setStartLongitude("116.39742");
        order.setEndLongitude("39.896213");
        order.setEstimatedAmount(BigDecimal.valueOf(156.56));
        redisTemplate.opsForValue().set("order:"+order.getOrderId(),JSONObject.toJSONString(order));
        OrderChanel.sendMessage(carCode, JSONObject.toJSONString(order));

        log.info("开始计时:,{}",new Date());
        rabbitTemplate.convertAndSend("taxi-order-delayEx", "", order.getOrderId().toString(), new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(30*1000);
                return message;
            }
        });

        return R.OK();
    }




}
